#include "EWAUIWidgetScroll.hpp"

namespace Eyer
{
    bool EWAUIWidgetScroll::MousePressEvent     (const Vec2I & pos)
    {
        RemoveAllWidget();
        offsetXY = scrollOffsetXY;
        AddWidget(contentLayout);
        contentLayout->SetChildCustomXY(pos);
        EWAUIWidget::MousePressEvent(pos);

        RemoveAllWidget();
        offsetXY.Zeros();
        AddWidget(progressBar);
        EWAUIWidget::MousePressEvent(pos);

        RemoveAllWidget();
        AddWidget(contentLayout);
        AddWidget(progressBar);

        return true;
    }

    bool EWAUIWidgetScroll::MouseReleaseEvent   (const Vec2I & pos)
    {
        RemoveAllWidget();
        offsetXY = scrollOffsetXY;
        AddWidget(contentLayout);
        contentLayout->SetChildCustomXY(pos);
        EWAUIWidget::MouseReleaseEvent(pos);

        RemoveAllWidget();
        offsetXY.Zeros();
        AddWidget(progressBar);
        EWAUIWidget::MouseReleaseEvent(pos);

        RemoveAllWidget();
        AddWidget(contentLayout);
        AddWidget(progressBar);

        return true;
    }

    bool EWAUIWidgetScroll::MouseMoveEvent      (const Vec2I & pos)
    {
        OnDraw();
        // EyerLog("%s pos x: %d, y: %d\n", widgetName.c_str(), pos.x(), pos.y());
        RemoveAllWidget();
        offsetXY = scrollOffsetXY;
        AddWidget(contentLayout);
        contentLayout->SetChildCustomXY(pos);
        EWAUIWidget::MouseMoveEvent(pos);

        RemoveAllWidget();
        offsetXY.Zeros();
        AddWidget(progressBar);
        EWAUIWidget::MouseMoveEvent(pos);

        RemoveAllWidget();
        AddWidget(contentLayout);
        AddWidget(progressBar);

        return true;
    }

    bool EWAUIWidgetScroll::OnScrollEvent       (const double x, const double y)
    {
        if (type == ScrollType::VERTICAL) {
            progressBar->MarginStartChange(progressBar->bar->marginTop - y * 2.0);
        }
        else if (type == ScrollType::VERTICAL_INVERT) {
            progressBar->MarginStartChange(progressBar->bar->marginTop - y * 2.0);
        }
        else if (type == ScrollType::HORIZONTAL) {
            progressBar->MarginStartChange(progressBar->bar->marginLeft - x * 2.0);
        }

        return true;
    }

    Vec2I EWAUIWidgetScroll::OnMeasure(int suggestWidth, int suggestHeight, int level)
    {
        // 滚动内容 Layout 和滚动组建需要分开进行计算
        RemoveAllWidget();
        AddWidget(contentLayout);
        if(type == ScrollType::HORIZONTAL){
            // 横向
            EWAUIWidget::OnMeasure(INT_MAX, suggestHeight, level);
        }
        else if(type == ScrollType::VERTICAL_INVERT){
            // 纵向
            EWAUIWidget::OnMeasure(suggestWidth, INT_MAX, level);
        }
        else if(type == ScrollType::VERTICAL){
            // 纵向
            EWAUIWidget::OnMeasure(suggestWidth, INT_MAX, level);
        }

        RemoveAllWidget();
        AddWidget(progressBar);
        Vec2I size = EWAUIWidget::OnMeasure(suggestWidth, suggestHeight, level);

        RemoveAllWidget();
        AddWidget(contentLayout);
        AddWidget(progressBar);

        return size;
    }

    int EWAUIWidgetScroll::Draw(int level)
    {
        std::vector<EyerSmartPtr<EWAUIGraph>> tempGraphList;

        offsetXY = scrollOffsetXY;
        RemoveAllWidget();
        AddWidget(contentLayout);
        EWAUIWidget::Draw(level);
        for(int i = 0; i < graphList.size(); i++){
            tempGraphList.push_back(graphList[i]);
        }
        contentLayout->SetChildCustomXY(scrollOffsetXY);

        offsetXY = Vec2I(0, 0);
        RemoveAllWidget();
        AddWidget(progressBar);
        EWAUIWidget::Draw(level);
        for(int i = 0; i < graphList.size(); i++){
            tempGraphList.push_back(graphList[i]);
        }

        RemoveAllWidget();
        AddWidget(contentLayout);
        AddWidget(progressBar);

        RemoveAllUIGraph();

        for(int i = 0; i < tempGraphList.size(); i++){
            graphList.push_back(tempGraphList[i]);
        }

        return 0;
    }

    int EWAUIWidgetScroll::OnDraw()
    {
        // 计算可滑动区域
        int scrollSize = 0;
        int contentSize = 0;
        if (type == ScrollType::VERTICAL) {
            scrollSize = wh.height();
            contentSize = contentLayout->wh.height();
        }
        else if (type == ScrollType::VERTICAL_INVERT) {
            scrollSize = wh.height();
            contentSize = contentLayout->wh.height();
        }
        else if (type == ScrollType::HORIZONTAL) {
            scrollSize = wh.width();
            contentSize = contentLayout->wh.width();
        }

        progressBar->SetSize(contentSize, scrollSize);
        int marginStart = progressBar->GetMaginStart();

        if (type == ScrollType::VERTICAL) {
            scrollOffsetXY.y(marginStart);
        }
        else if (type == ScrollType::VERTICAL_INVERT) {
            int mTop = std::max(0, scrollSize - contentSize);
            scrollOffsetXY.y(marginStart + mTop);
        }
        else if (type == ScrollType::HORIZONTAL) {
            scrollOffsetXY.x(marginStart);
        }
        return 0;
    }

    EWAUIWidgetScroll::EWAUIWidgetScroll(ScrollType _type, EWAUILayoutType _contentlayoutType, EWAUILayoutParams _width, EWAUILayoutParams _height)
        : EWAUIWidget(_width, _height)
        , type (_type)
        , scrollOffsetXY (0, 0)
        , progressBar(nullptr)
        , contentLayout(nullptr)
    {
        SetWidgetName("EWAUIWidgetScroll");
        SetLayoutType(EWAUILayoutType::RetativeLayout);

        if (type == ScrollType::VERTICAL) {
            EyerSmartPtr<EWAUIProgressBar> progressBarTemp(new EWAUIProgressBar(BarType::VERTICAL, LP::WRAP_CONTENT, LP::MATCH_PARENT));
            progressBar = progressBarTemp;

            EyerSmartPtr<EWAUILayout> contentLayoutTemp(new EWAUILayout(_contentlayoutType, LP::MATCH_PARENT, LP::WRAP_CONTENT));
            contentLayout = contentLayoutTemp;
        }
        else if (type == ScrollType::VERTICAL_INVERT) {
            EyerSmartPtr<EWAUIProgressBar> progressBarTemp(new EWAUIProgressBar(BarType::VERTICAL, LP::WRAP_CONTENT, LP::MATCH_PARENT));
            progressBar = progressBarTemp;

            EyerSmartPtr<EWAUILayout> contentLayoutTemp(new EWAUILayout(_contentlayoutType, LP::MATCH_PARENT, LP::WRAP_CONTENT));
            contentLayout = contentLayoutTemp;
        }
        else if (type == ScrollType::HORIZONTAL) {
            EyerSmartPtr<EWAUIProgressBar> progressBarTemp(new EWAUIProgressBar(BarType::HORIZONTAL, LP::MATCH_PARENT, LP::WRAP_CONTENT));
            progressBar = progressBarTemp;

            EyerSmartPtr<EWAUILayout> contentLayoutTemp(new EWAUILayout(_contentlayoutType, LP::MATCH_PARENT, LP::WRAP_CONTENT));
            contentLayout = contentLayoutTemp;
        }

        contentLayout->SetBGColor(EyerColor::NO_COLOR);
        contentLayout->SetWidgetName("EWAUIWidgetScroll_ContentLayout");

        if (type == ScrollType::VERTICAL || type == ScrollType::VERTICAL_INVERT) {
            progressBar->alignParentRight = true;
        }
        else if (type == ScrollType::HORIZONTAL) {

        }

        AddWidget(contentLayout);
        AddWidget(progressBar);
    }

    EWAUIWidgetScroll::~EWAUIWidgetScroll()
    {

    }

    int EWAUIWidgetScroll::AddContentWidget(const EyerSmartPtr<EWAUIWidget> & widget)
    {
        contentLayout->AddWidget(widget);
        return 0;
    }
}